package org.fude.configuration;

import javax.annotation.Resource;

import org.fude.entity.MySqlData;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import com.alibaba.druid.pool.DruidDataSource;

@Configuration
public class MybaticConfiguration {
	
	@Resource(name="mysqlData")
	private MySqlData data;
	
	@Bean("mysqlData")
	@ConfigurationProperties(prefix = "spring.datasource")
	MySqlData mySqlData() {
		return new MySqlData();
	}
	
	@Bean("dDataSource")
	@Primary
	DruidDataSource dataSource() {
		DruidDataSource dataSource = new DruidDataSource();
		dataSource.setUrl(data.getUrl());
		dataSource.setUsername(data.getUsername());
		dataSource.setPassword(data.getPassword());
		dataSource.setDriverClassName(data.getDriverClassName());
		return dataSource;
	}
	
	@Bean
	SqlSessionFactoryBean sessionFactory() {
		SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
		sessionFactory.setDataSource(dataSource());
		return sessionFactory;
	}
	@Bean
	DataSourceTransactionManager dataSourceTransactionManager() {
		DataSourceTransactionManager manager = new DataSourceTransactionManager();
		manager.setDataSource(dataSource());
		return manager;
	}
}
